package com.ruoyi.book.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.book.domain.TLeft;
import com.ruoyi.book.service.ITLeftService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图书借阅表Controller
 * 
 * @author ruoyi
 * @date 2022-01-04
 */
@RestController
@RequestMapping("/book/left")
public class TLeftController extends BaseController
{
    @Autowired
    private ITLeftService tLeftService;

    /**
     * 查询图书借阅表列表
     */
    @PreAuthorize("@ss.hasPermi('book:left:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLeft tLeft)
    {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            tLeft.setUserId(userId);
        }
        startPage();
        List<TLeft> list = tLeftService.selectTLeftList(tLeft);
        return getDataTable(list);
    }

    /**
     * 导出图书借阅表列表
     */
    @PreAuthorize("@ss.hasPermi('book:left:export')")
    @Log(title = "图书借阅表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TLeft tLeft)
    {
        List<TLeft> list = tLeftService.selectTLeftList(tLeft);
        ExcelUtil<TLeft> util = new ExcelUtil<TLeft>(TLeft.class);
        util.exportExcel(response, list, "图书借阅表数据");
    }

    /**
     * 获取图书借阅表详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:left:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tLeftService.selectTLeftById(id));
    }

    /**
     * 新增图书借阅表
     */
    @PreAuthorize("@ss.hasPermi('book:left:add')")
    @Log(title = "图书借阅表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLeft tLeft)
    {
        return toAjax(tLeftService.insertTLeft(tLeft));
    }

    /**
     * 修改图书借阅表
     */
    @PreAuthorize("@ss.hasPermi('book:left:return')")
    @GetMapping("/returnLeftById/{id}")
    public AjaxResult returnLeftById(@PathVariable("id")Long id)
    {
        TLeft left =new TLeft();
        left.setId(id);
        return toAjax(tLeftService.returnBook(left));
    }

    /**
     * 修改图书借阅表
     */
    @PreAuthorize("@ss.hasPermi('book:left:edit')")
    @Log(title = "图书借阅表", businessType = BusinessType.UPDATE)
    @GetMapping("/updateLeftById/{id}")
    public AjaxResult updateLeftById(@PathVariable("id")Long id)
    {
        TLeft left =new TLeft();
        left.setId(id);
        return toAjax(tLeftService.updateTLeft(left));
    }

    /**
     * 删除图书借阅表
     */
    @PreAuthorize("@ss.hasPermi('book:left:remove')")
    @Log(title = "图书借阅表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tLeftService.deleteTLeftByIds(ids));
    }

    /**
     * 借阅图书
     */
    @PreAuthorize("@ss.hasPermi('book:info:left')")
    @GetMapping("/addLeftByBookId/{bookId}")
    public AjaxResult addLeftByBookId(@PathVariable("bookId")Long bookId)
    {
        TLeft left = new TLeft();
        left.setBookId(bookId);
        return toAjax(tLeftService.insertTLeft(left));
    }
}
